﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Web.UI.WebControls;
using UniversityStudentManagemnetSystem.BLL;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.UI
{
    public partial class CourseAssignToTeacher : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Account/Login.aspx", false);
            }

            if (IsPostBack!=true)
            {
                PopulatedDepartmentDropDownList();
            }
        }
        Teacher aTeacher = new Teacher();
        TeacherManager teacherManager = new TeacherManager();
        CourseManager courseManager = new CourseManager();
        private DepartmentManager departmentManager;
        CreditManager creditManager= new CreditManager();
        CourseAssignManager courseAssignManager = new CourseAssignManager();
        private void PopulatedDepartmentDropDownList()
        {
            try
            {
                departmentManager = new DepartmentManager();
                DataTable departments = departmentManager.GetAllDepartments();
                departmentDropDownList.DataTextField = "Name";
                departmentDropDownList.DataValueField = "Id";
                departmentDropDownList.DataSource = departments;
                departmentDropDownList.DataBind();
            }
            catch (Exception exception)
            {

                assignMessageLabel.Text = exception.Message;
            }
        }

        protected void teacherDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedTeacher = Convert.ToInt32(teacherDropDownList.SelectedValue);
            Teacher aTeacher = new Teacher();
            aTeacher.Id = selectedTeacher;

            creditManager = new CreditManager();
            try
            {
                Credit credit = creditManager.GetCreditsOf(aTeacher);
                assignCreditTextBox.Text = credit.AssignedCredits.ToString();
                remainingCreditTextBox.Text = credit.RemainingCredits.ToString();
            }
            catch (Exception exception)
            {
                assignMessageLabel.Text = exception.Message;
            }
        }
        protected void departmentDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int selectedDepartment = Convert.ToInt32(departmentDropDownList.SelectedValue);
                
                PopulateCourseDropDownListOf(selectedDepartment);
                PopulateTeacherDropDownListOf(selectedDepartment);
            }
            catch (Exception exception)
            {
                assignMessageLabel.Text = exception.Message;
            }
        }

        private void PopulateCourseDropDownListOf(int selectedDepartment)
        {
            DataTable cousreTable = courseManager.GetAllCoursesOf(selectedDepartment);
            courseCodeDropDownList.DataTextField = "Code";
            courseCodeDropDownList.DataValueField = "Id";
            courseCodeDropDownList.DataSource = cousreTable;
            courseCodeDropDownList.DataBind();
        }

        private void PopulateTeacherDropDownListOf(int selectedDepartment)
        {
            DataTable teacherDataTable = teacherManager.GetAllTeachersFor(selectedDepartment);
            teacherDropDownList.DataTextField = "Name";
            teacherDropDownList.DataValueField = "Id";
            teacherDropDownList.DataSource = teacherDataTable;
            teacherDropDownList.DataBind();
        }
        protected void assignButton_Click(object sender, EventArgs e)
        {
            try
            {
                SetCourseToTeacher();
            }
                catch(FormatException exception)
                {
                    assignMessageLabel.ForeColor = Color.Red;
                    assignMessageLabel.Text = exception.Message;

                }
            catch (Exception exception)
            {
                assignMessageLabel.ForeColor = Color.Red;
                assignMessageLabel.Text = exception.Message;
            }

            ClearAllTextBoxes();
        }

        private void ClearAllTextBoxes()
        {
            assignCreditTextBox.Text = string.Empty;
            remainingCreditTextBox.Text = string.Empty;
            nameTextBox.Text = string.Empty;
            creditTextBox.Text = string.Empty;
        }

        private void SetCourseToTeacher()
        {
            
            aTeacher.Id = Convert.ToInt32(teacherDropDownList.SelectedValue);
            aTeacher.TeacherName = teacherDropDownList.SelectedItem.ToString();
            
            aTeacher.Course.CourseId = Convert.ToInt32(courseCodeDropDownList.SelectedValue);
            
            if (assignCreditTextBox.Text != "" && remainingCreditTextBox.Text != ""  && nameTextBox.Text != "" && creditTextBox.Text != "")
            {
                try
                {
                    courseAssignManager = new CourseAssignManager();
                    string mesg = courseAssignManager.SetCourseTo(aTeacher);
                    assignCreditTextBox.ForeColor = Color.HotPink;
                    assignMessageLabel.Text = mesg;
                }
                catch (Exception exception)
                {
                    assignMessageLabel.Text = exception.Message;
                }
            }
            else
            {
                assignCreditTextBox.ForeColor = Color.Red;
                assignMessageLabel.Text = "Please select the list box correctly.";
            }


        }

     


        protected void courseCodeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetUnassignedCourse();
        }

        private void GetUnassignedCourse()
        {
            
            int selectedCourse = Convert.ToInt32(courseCodeDropDownList.SelectedValue);
            object returnedOject = courseAssignManager.GetCourseOrCourseTeacher(selectedCourse);

            if (returnedOject is Teacher)
            {
                Teacher aTeacher = returnedOject as Teacher;
                assignMessageLabel.Text = string.Format("This course is already assigned to {0}, \n {1} of {2} Department.",
                                                        aTeacher.TeacherName, aTeacher.Designation.DesignationName,
                                                        aTeacher.Department.Name);
                ViewState["teacherNotAssined"] = false;
            }
            else if (returnedOject is Course)
            {
                Course aCourse = returnedOject as Course;
                nameTextBox.Text = aCourse.CourseName;
                creditTextBox.Text = aCourse.Credit;
                ViewState["teacherNotAssined"] = true;
            }
        }
    }
}